//
//  WelcomeViewController.swift
//  weibo117
//
//  Created by 宋禹奇 on 15/6/28.
//  Copyright © 2015年 宋禹奇. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let urlString = sharedUserAccount?.avatar_large{
            //设置头像
            iconView.sd_setImageWithURL(NSURL(string: urlString))
        
        }
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        bootomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 200)
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            
                self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(1.0, animations: { () -> Void in
                    self.messageLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                       
                        NSNotificationCenter.defaultCenter().postNotificationName(YUSwitchRootVCNotification, object: true)
                        
                        
                })
        }
        
        
    }
    /// 头像的底部约束
    var bootomConstraint: NSLayoutConstraint?
    
    
    override func loadView() {
    
        view = UIView()
        
        let backImageView = UIImageView(image: UIImage(named: "ad_background"))
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        
        //自动布局
        backImageView.translatesAutoresizingMaskIntoConstraints = false

        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics:nil , views: ["subView":backImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":backImageView]))
        
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(90)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":iconView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(90)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":iconView]))
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -160))
        
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8))
        
        
    }

    lazy var iconView:UIImageView = {
    
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        iconView.layer.masksToBounds = true
        iconView.layer.cornerRadius = 45
        
        return iconView
    
    }()
    
    lazy var messageLabel:UILabel = {
    
        let label = UILabel()
        
        label.text = "欢迎归来"
        
        label.sizeToFit()
        
        label.alpha = 0
        
        return label
    
    }()
    
}
